package travelDream.beans;

import java.util.List;

import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

import traveldream.ejb.DTO.HotelDTO;
import traveldream.ejb.interfaces.HotelBeanLocal;
import traveldream.ejb.interfaces.NoDeleteException;
import traveldream.ejb.interfaces.NotFindException;


@ManagedBean(name = "HotelManaged")
@ViewScoped
public class HotelManaged {
	private List<HotelDTO> hDTO;
	private List<HotelDTO> filteredHotel;
	private HotelDTO viewedH;
	@EJB
	private HotelBeanLocal hotelbeanlocal;
	

	public HotelManaged(){}
	
	public void allHotel(){
		hDTO = hotelbeanlocal.getAllHotel();
	}
	
	public void searchById() {
		FacesContext facesContext = FacesContext.getCurrentInstance();
		int passedParameter = Integer.parseInt((facesContext.getExternalContext().getRequestParameterMap().get("id")));
		try{
		viewedH = hotelbeanlocal.find(passedParameter);
		}
		catch(NotFindException e){
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Error", "Please, use the menu to go back to hotels and try again!!"));
		}
	}
	
	public void searchById(int id) {
		try{
		viewedH = hotelbeanlocal.find(id);
		}
		catch(NotFindException e){
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Error", "Please, use the menu to go back to hotels and try again!!"));
		}
	}
	
	public String changePage(int id){
		return "hotelView.xhtml?faces-redirect=true&id="+id;
	}
	public void handleError(){
		FacesContext facesContext = FacesContext.getCurrentInstance();
		String passedParameter = (facesContext.getExternalContext().getRequestParameterMap().get("error"));
		if(passedParameter==null) return;
		if(passedParameter.compareTo("nullPointer")==0)
		{
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Error", "An error occured, please try again!!"));
		}
	}
	
	public String editPage(int id){
		return "hotelEdit.xhtml?faces-redirect=true&id="+id;
	}
	
	public String delete(int id){
		try{
		hotelbeanlocal.delete(id);
		}
		catch(NotFindException e){
			return "hotel.xhtml?faces-redirect=true&error=nullPointer";
		}
		catch(NoDeleteException e){
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Error", "You cannot delete!! Please delete fisrt the package in which it is"));
			return "";
		}
		return "hotel.xhtml?faces-redirect=true";
	}
	
	public List<HotelDTO> gethDTO() {
		return hDTO;
	}

	public void sethDTO(List<HotelDTO> hDTO) {
		this.hDTO = hDTO;
	}

	public HotelBeanLocal getHotelbeanlocal() {
		return hotelbeanlocal;
	}

	public void setHotelbeanlocal(HotelBeanLocal hotelbeanlocal) {
		this.hotelbeanlocal = hotelbeanlocal;
	}

	public HotelDTO getViewedH() {
		return viewedH;
	}

	public void setViewedH(HotelDTO viewedH) {
		this.viewedH = viewedH;
	}

	public List<HotelDTO> getFilteredHotel() {
		return filteredHotel;
	}

	public void setFilteredHotel(List<HotelDTO> filteredHotel) {
		this.filteredHotel = filteredHotel;
	}
	
	
	
}
